package com.mac.bluetoothbt.feature;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

/**
 * 授权时间
 *
 * @author wyh
 */
public class RemainTime extends AppCompatActivity {
    private final int oneSecond = 1_000;
    private int remainTime;
    private String password;
    private int passwordlength;
    private EditText remaintimeEditText, passwordEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting6);
        Intent intent = getIntent();
        remainTime = intent.getIntExtra("remaintime", -1);
        password = intent.getStringExtra("password");
        passwordlength = password.length();
        if (remainTime >= 0) {
            remaintimeEditText = findViewById(R.id.remaintime);
            remaintimeEditText.setText(String.format(Locale.PRC, "%d", remainTime));
        }
        passwordEditText = findViewById(R.id.password);
        passwordEditText.setText(password);
    }

    public void confirmOnClicked(View view) throws InterruptedException {
        password = passwordEditText.getText().toString();
        remainTime = Integer.parseInt(remaintimeEditText.getText().toString());

        if (password.length() != passwordlength) {
            Toast.makeText(this,
                    String.format(Locale.PRC, "错误,应输入%d位密码", passwordlength),
                    Toast.LENGTH_LONG).show();
            return;
        }

        if (passwordlength == 6) {
            FrameUtil.setPassword6(getApplicationContext(), password);
            Thread.sleep(oneSecond);
            FrameUtil.setRemainTime6(getApplicationContext(), remainTime);
            Thread.sleep(oneSecond);
            FrameUtil.sendAfterAllwrite(getApplicationContext());
            startActivity(new Intent().setClass(this, Main2Activity.class));
        } else if (passwordlength == 8) {
            FrameUtil.setPassword8(getApplicationContext(), password);
            Thread.sleep(oneSecond);
            FrameUtil.setRemainTime8(getApplicationContext(), remainTime);
            Thread.sleep(oneSecond);
            FrameUtil.sendAfterAllwrite(getApplicationContext());
            startActivity(new Intent().setClass(this, Main2Activity.class));
        }
    }

    public void cancelOnClicked(View view) {
        startActivity(new Intent().setClass(this, Main2Activity.class));
    }
}
